package com.dd.ent.car.ui.detail;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.dd.ent.car.R;
import com.dd.ent.car.common.bean.video.Video;
import com.dd.ent.car.service.user.UserService;
import com.dd.ent.car.ui.user.LoginActivity;
import com.dd.ent.car.ui.widget.CommentDialog;
import com.pp.ent.Cherry;
import com.pp.ent.view.annotation.Inject;

/**
 * Created by dengqu on 17/8/19.
 */

public class DetailBottomView extends FrameLayout implements View.OnClickListener {
    private CommentDialog mCommentDialog;
    private View commentView;
    private SendCommentListener sendCommentListener;
    @Inject
    private UserService userService;

    public DetailBottomView(Context context) {
        super(context);
        init(context);
    }

    public DetailBottomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        View root = LayoutInflater.from(context).inflate(R.layout.detail_common_bottom_view, this, true);
        commentView = root.findViewById(R.id.lyt_write_comment);
        commentView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (userService.isLogin()) {
                    showCommentDialog();
                } else {
                    LoginActivity.startActivity(getContext());
                }
            }
        });
    }

    private void showCommentDialog() {
        if (mCommentDialog == null) {
            mCommentDialog = new CommentDialog(getContext());
            mCommentDialog.setOnSendButtonClickListener(this);
        }
        if (!mCommentDialog.isShowing()) {
            mCommentDialog.show();
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        Cherry.injectObject(this);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        Cherry.remove(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_send:
                handleSendComment(v);
                break;
        }

    }


    private void handleSendComment(View v) {
        if (mCommentDialog != null) {
            if (sendCommentListener != null) {
                sendCommentListener.onSend(mCommentDialog.getContent());
            }
            mCommentDialog.dismiss();
            mCommentDialog = null;
        }

    }

    public void hideCommentDialog() {
        if (mCommentDialog != null) {
            mCommentDialog.dismiss();
            mCommentDialog = null;
        }
    }

    public void setSendCommentListener(SendCommentListener sendCommentListener) {
        this.sendCommentListener = sendCommentListener;
    }

    public interface SendCommentListener {
        public void onSend(CharSequence content);
    }
}
